#### FIGURE 2: STUDY 3 PERSISTENCE
#### Two waves for study 3 looking at the two main outcomes

rm(list = ls())
source("./2_code/00_setup.R")

#### STUDY 3, WAVE 1 ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)
data3$employed <- as.factor(data3$employed)

# Support for deportation
lin_entry_economic3 <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                                edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_entry_humanitarian3 <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                    edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])

# Prejudice against migrants
lin_prejudice_economic3 <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                    edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian3 <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                        edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 2 ####

data3b <- fread(paste0(data_path, "data_study3_wave2.csv"), header = TRUE)

# Support for deportation
lin_entry_economic3b <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                                 edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_entry_humanitarian3b <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                     edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])

# Prejudice against migrants
lin_prejudice_economic3b <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                     edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian3b <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                         edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])


#### PREPARE DATA FOR PLOTTING ####

### Economic treatment, open migration policy ###

out_economic <- rbind(
  tidy(lin_entry_economic3) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error),
  tidy(lin_entry_economic3b) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error)
)

types_economic <- data.frame(
  treatment = rep('economic', 2),
  Study = c('Wave 1', 'Wave 2')
)

out_economic2 <- cbind(out_economic, types_economic)


### Humanitarian treatment, open migration policy ###

out_humanitarian <- rbind(
  tidy(lin_entry_humanitarian3) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error),
  tidy(lin_entry_humanitarian3b) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error)
)

types_humanitarian <- data.frame(
  treatment = rep('humanitarian', 2),
  Study = c('Wave 1', 'Wave 2')
)

out_humanitarian2 <- cbind(out_humanitarian, types_humanitarian)


### Economic treatment, feelings ###

out_economic_feel <- rbind(
  tidy(lin_prejudice_economic3) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error),
  tidy(lin_prejudice_economic3b) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error)
)

out_economic_feel2 <- cbind(out_economic_feel, types_economic)


### Humanitarian treatment, feelings ###

out_humanitarian_feel <- rbind(
  tidy(lin_prejudice_humanitarian3) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error),
  tidy(lin_prejudice_humanitarian3b) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error)
)

out_humanitarian_feel2 <- cbind(out_humanitarian_feel, types_humanitarian)


#### PLOTTING ####

fla <- rbind(
  out_economic2,
  out_humanitarian2,
  out_economic_feel2,
  out_humanitarian_feel2
)

data_plot <- data.frame(
  fla,
  Outcome = c(rep("ATE Open Migration", 2),
              rep("ATE Open Migration", 2),
              rep("ATE Positive Affect", 2),
              rep("ATE Positive Affect", 2)),
  Treatment = c(rep("Economic Narrative", 2), rep("Humanitarian Narrative", 2),
                rep("Economic Narrative", 2), rep("Humanitarian Narrative", 2))
)

# Reorder factors
data_plot$Study <- factor(data_plot$Study, levels = c("Wave 2", "Wave 1"))
data_plot$Outcome <- factor(data_plot$Outcome, levels = c("ATE Positive Affect", "ATE Open Migration"))

plot_waves <- ggplot(data_plot, aes(y = Outcome, x = estimate, color = Study, group = Study)) +
  geom_errorbarh(aes(xmin = estimate - 1.96 * std.error, xmax = estimate + 1.96 * std.error), 
                 height = 0, position = position_dodge(width = 0.3), linewidth = 0.9) +
  geom_errorbarh(aes(xmin = estimate - 1.645 * std.error, xmax = estimate + 1.645 * std.error), 
                 height = 0, position = position_dodge(width = 0.3), linewidth = 1.9) +
  geom_point(size = 3, position = position_dodge(width = 0.3)) +
  facet_grid(~ Treatment) +
  theme_minimal() +
  ylab("") + xlab("") +
  scale_x_continuous(limits = c(-0.2, 0.3)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  guides(color = guide_legend(reverse = TRUE)) +
  labs(color = "Study 3") +
  theme(
    strip.text = element_text(size = 16),
    axis.text.y = element_text(size = 13),
    axis.text.x = element_text(size = 13),
    legend.text = element_text(size = 13),
    legend.title = element_text(size = 13)
  )

plot_waves

ggsave(filename = paste0(plot_path, "figure_02.png"), plot = plot_waves, width = 10, height = 5, dpi = 600)